﻿using Kingdee.BOS.WebApi.Client;
using Kingdee.BOS.WebApi.DataEntify;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using System.Threading.Tasks;
using System.Windows.Forms;
using FastReport;
using System.IO;
using biaoqian.CSwnjian;
using Infragistics.Documents.Excel.Charts;

namespace biaoqian
{
    public partial class Zhuye : Form
    {
        Class1 one = new Class1();
        BiaoQian BQ = new BiaoQian();
        LDhuoyunEntities LD=new LDhuoyunEntities();
        public Zhuye()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DateTime now = DateTime.Now;
            string currentDateTime = now.ToString("yyyy-MM-dd HH:mm:ss");
            K3CloudApi client = new K3CloudApi();
            //用于记录结果
            StringBuilder Info = new StringBuilder();
            //业务对象标识
            string formId = "SAL_DELIVERYNOTICE";
            //查询字段集合，即返回哪些数据，不能为空，根据不同业务单据填写不同的字段名，以下仅为示例
            string fieldKeys = "FBillNo,FCustomerID.FName,FMaterialID.FNumber,FMaterialName,FMateriaModel,FQty,FPriceUnitId.FName,F_SMGS_FHDate,FSaleOrgId.FName";
            //请求参数，示例使用的是SDK提供的模板类，还可以使用字符串拼接等方式  and FCancelStatus='A'  and FPlanDeliveryDate >'" + currentDateTime + "'
            var ZBLx = comboBox1.Text;
            var Wuli = textBox5.Text;
            var param = new QueryParam()
            {
                FormId = formId,
                FieldKeys = fieldKeys,
                FilterString = "FBillNo like'%" + ZBLx + "%'and FSaleOrgId.FName='苏州安派思包装有限公司' and FMaterialName like'%"+ Wuli + "%'and FMaterialID.FNumber not  like '12.10.11%'",
                TopRowCount = 0,
                StartRow = 0,
                Limit = 120,
                OrderString = "FBillNo",
            };
            //调用接口
            var result = client.ExecuteBillQuery(param.ToJson());
            /* JsonSerializerSettings settings = new JsonSerializerSettings();
             string jsongStr = JsonConvert.SerializeObject(result, settings);*/
            //转换成标准文档并遍历
            var dataTable2 = one.DAtaijs();
            var ssjinf = 0;
            var shulian = this.textBox1.Text == "" ? "0" : this.textBox1.Text;
            //遍历填充时间
            for (int i = 1; i < result.Count; i++)
            {
                var shuj = result[i][7]==null?"": result[i][7].ToString(); //时间
                if (shuj=="")
                {
                    result[i][7] = result[i - 1][7];
                }
            }

            //去重
            foreach (var item in result)
            {
                ssjinf++;
                var owulia = item[3].ToString();
                var TImes = item[7]==null? "" :item[7].ToString();
                DataRow[] dataRow = dataTable2.Select("Material='" + owulia + "' and SHIjian='"+ TImes + "'");
                if (dataRow.Count() > 0)
                {
                    var shnd = int.Parse(item[5].ToString());
                    foreach (var item2 in dataRow)
                    {
                        var shu = int.Parse(item2[5].ToString());
                        shu += shnd;
                        item2[5] = shu;
                    }
                }
                else
                {
                    dataTable2.Rows.Add(item[0], item[1], item[2], item[3], item[4], item[5], item[6], item[7]);
                }
            }
            DataTable dataTable23 = one.DAtaijs();
            //行号过滤
            if (shulian != "0")
            {
                var ji = int.Parse(shulian);
                DataRow kol = dataTable2.Rows[ji - 1];//符合条件的行
                dataTable23.Rows.Add(kol.ItemArray);
            }
            else
            {
                dataTable23 = dataTable2.Copy();
            }
            
            //返回前台
            comboBox1.Items.Clear();
            foreach (DataRow item in dataTable23.Rows)
            {
                comboBox1.Items.Add(item["encoding"].ToString());
            }
            dataGridView1.DataSource = dataTable23;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            applisj.values = comboBox1.Text;
            applisj.IDs = textBox1.Text;

            //打印标签
            DataTable dataTable1 = one.Wuliao();
            var id = applisj.values;
            var shu = applisj.IDs;
            var wulaios = textBox5.Text;
            DataTable dataTable2 = BQ.NewMs(id, ref shu, wulaios);//返回需要打印的标签信息
                                                        //------------------------------------------------------------
            List<List<object>> BiasoR = BQ.NewMethod();//返回物料的对饮大小标签

            var FE = BiasoR[0][0].ToString();//大标签
            var FES = BiasoR[1][0].ToString();//小标签
            var path = "";//打开的标签
                          //再分辨物料的大小标签
            path = BQ.biaoqinazhi(dataTable2, FE, FES, path);
            if (path=="")
            {
                MessageBox.Show("该行不属于眼睛标签！", "弹窗标题", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            dataTable1 = BQ.Wuliaos(dataTable2);//返回的数据
            DataRow rows = dataTable1.Rows[0];
            DataRow row = dataTable2.Rows[0];
            string lai = Convert.ToString(rows["GM_PART_NUM"]);
            var ling = Convert.ToInt16(rows["QTY"]);
            int sdk;
            string s = row["quantity"].ToString();
            int d = s == "" ? 1 : Convert.ToInt16(row["quantity"]);
            sdk = ling == 0 ? 0 : d / Convert.ToInt16(rows["QTY"]);
            rows["COUNTRY_ORIGIN_DW"] = sdk;//打印次数
            DataSet da = new DataSet();
            da.Tables.Add(dataTable1);
            dataTable1.TableName = "Table1";

            //防伪码
            //string baseDirectory1 = Application.StartupPath;
            string physicalPath1 = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Biaoqina.Fangwei );
            Report report1 = new Report();
            report1.Load(physicalPath1);//打开文件/*
            report1.RegisterData(da);//绑定数据源
                                               //webReport.Report.SetParameterValue("ksnfjidn", Appbian);//替换fastreport参数的值--
            DataBand data1 = report1.Report.FindObject("Data1") as DataBand;
            data1.DataSource = report1.Report.GetDataSource("Table1");
            report1.PrintSettings.Printer = "Fax";
            report1.PrintSettings.Copies = sdk;
            report1.Prepare();
            report1.ShowPrepared();
            report1.Dispose();

            //标签码
            //string baseDirectory = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;
            string physicalPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path);
            Report report = new Report();
            report.Load(physicalPath);//打开文件/*
            report.RegisterData(da);//绑定数据源
                                              //webReport.Report.SetParameterValue("ksnfjidn", Appbian);//替换fastreport参数的值--
            DataBand data = report.Report.FindObject("Data1") as DataBand;
            data.DataSource = report.Report.GetDataSource("Table1");
            report.PrintSettings.Printer = "Fax";
            report.PrintSettings.Copies = sdk;
            report.Prepare();
            report.ShowPrepared();
            report.Dispose();
        }

        private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
           
        }

        private void comboBox1_KeyUp(object sender, KeyEventArgs e)
        {

        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
               e.RowBounds.Location.Y,
               dataGridView1.RowHeadersWidth - 4,
               e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
                dataGridView1.RowHeadersDefaultCellStyle.Font,
                rectangle,
                dataGridView1.RowHeadersDefaultCellStyle.ForeColor,
                TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

        private void Zhuye_Load(object sender, EventArgs e)
        {
            this.textBox2.Hide();
            this.textBox3.Hide();
            this.textBox4.Hide();
            this.label3.Hide();
            this.label4.Hide();
            this.label5.Hide();
            Biaoqina.Fangwei =this.textBox2.Text;
            Biaoqina.BiaoqianA = this.textBox3.Text;
            Biaoqina.BiaoqianB = this.textBox4.Text;
        }

        private void Button3_Click(object sender, EventArgs e)
        {
            
            if (this.textBox2.Visible==true)
            {
                this.textBox2.Hide();
                this.textBox3.Hide();
                this.textBox4.Hide();
                this.label3.Hide();
                this.label4.Hide();
                this.label5.Hide();
            }
            else
            {
                this.textBox2.Visible=true;
                this.textBox3.Visible = true;
                this.textBox4.Show();
                this.label3.Show();
                this.label4.Show();
                this.label5.Show();
            }
        }
        private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
        {

        }

        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            Biaoqina.Fangwei = this.textBox2.Text;
            Biaoqina.BiaoqianA = this.textBox3.Text;
            Biaoqina.BiaoqianB = this.textBox4.Text;
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count > 0)
            {
                this.textBox1.Text = (this.dataGridView1.SelectedRows[0].Index+1).ToString();
            }
        }
    }
}
